package com.xinghou.platform.server.api.controller;

import java.sql.Timestamp;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xinghou.platform.util.*;
import com.xinghou.platform.util.exception.DBExceptionEnums;
import com.xinghou.platform.util.exception.ServiceException;
import com.xinghou.platform.server.api.log.ApiLogger;
import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.xinghou.platform.entity.req.MsgInfoReq;
import com.xinghou.platform.entity.req.ShareInfoReq;
import com.xinghou.platform.entity.req.UserInfoReq;
import com.xinghou.platform.entity.resp.ShareInfoListResp;
import com.xinghou.platform.entity.resp.ShareInfoResp;
import com.xinghou.platform.entity.resp.UserInfoListResp;
import com.xinghou.platform.entity.resp.UserInfoResp;
import com.xinghou.platform.server.api.bussiness.UserActionBusinessService;
import com.xinghou.platform.server.api.common.Constants;
import com.xinghou.platform.server.api.log.LoggerFactory;

/**
 * 最近来访用户
 *
 * @author wzk
 */
@Controller
public class UserActionsController extends BaseControl {
    private String key = ConfigUtil.getProperty("HTTP_SERVICE_KEY");
    @Resource
    private UserActionBusinessService userActionBusinessService;

    /**
     * 最近来访信息
     *
     * @param msgInfoReq
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/lastview", method = {RequestMethod.POST})
    public String getLastviewphoto(@RequestBody MsgInfoReq msgInfoReq,
                                   HttpServletRequest request, HttpServletResponse response) {
        ShareInfoListResp shareInfoListResp = new ShareInfoListResp();
        try {
            String fromno = msgInfoReq.getFromno();
            Long time = null;
            //TOD 不需要分页
            if (fromno.equals("-1")) {
                time = System.currentTimeMillis();
            } else {
                time = Long.parseLong(fromno);
            }
            List<ShareInfoResp> shareInfoRespList = userActionBusinessService.getVisitToPhoto(Integer.valueOf(msgInfoReq.getUserid()), time, Constants.RECENTLY_VISIT_PHOTONUM);
            shareInfoListResp.setDatalist(shareInfoRespList);
        } catch (Exception e) {
            e.printStackTrace();
            shareInfoListResp.setErrmsg("server interface error");
        }
        String repjson = JsonUtil.toJson(shareInfoListResp);
        return response(shareInfoListResp);
    }

    /**
     * 最近来访列表信息查询
     *
     * @param msgInfoReq
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/lastviewlist", method = {RequestMethod.POST})
    public String getVisitUserList( @RequestBody MsgInfoReq msgInfoReq,
                                   HttpServletRequest request, HttpServletResponse response) {
        UserInfoListResp userInfoListResp = new UserInfoListResp();
        try {
            Long time = System.currentTimeMillis();
            Integer pagesize=30;
            if(!StringUtils.isEmpty(msgInfoReq.getPagesize()))
            {
                pagesize=Integer.parseInt(msgInfoReq.getPagesize());
            }
            List<UserInfoResp> userInfoRespList = userActionBusinessService.getVisitToOrFromList(
                    Integer.valueOf(msgInfoReq.getUserid()), 1, time, pagesize);
            int size = userInfoRespList.size();
            userInfoListResp.setDatalist(userInfoRespList);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(DBExceptionEnums.SERVER_INTEERFACE_ERROR);
        }
        String repjson = JsonUtil.toJson(userInfoListResp);
        return response(userInfoListResp);
    }

    /**
     * 点赞或浏览用户列表查询
     *
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/findviewuserlist", method = {RequestMethod.POST})
    public String getBrowsePrainseUserList(@RequestBody ShareInfoReq shareInfoReq,
                                           HttpServletRequest request, HttpServletResponse response) {
        UserInfoListResp userInfoListResp = new UserInfoListResp();
        try {
            userInfoListResp.setResult("0");
            String fromno = shareInfoReq.getFromno();
            int querytype = Integer.valueOf(shareInfoReq.getType());
            if (querytype == Constants.BROWSE)//根据时间排序
            {
                if (fromno.equals("-1")) {    //第一次请求
                    fromno = DateUtils.changeTimestamp(System.currentTimeMillis() / 1000 + 60 * 60).toString();
                } else {
                    fromno = DateUtils.transDateToTimeStamp(fromno).toString();
                }
            } else if (querytype == Constants.PRAISE)//根据id排序
            {
                if (fromno.equals("-1")) {    //第一次请求
                    fromno = String.valueOf(Integer.MAX_VALUE);
                }
            }
            List<UserInfoResp> userInfoRespList = userActionBusinessService.getBrowseAndPraiseList(
                    Integer.valueOf(shareInfoReq.getUserid()), Integer.valueOf(shareInfoReq.getShareid()),
                    Integer.valueOf(shareInfoReq.getSharetype()), Integer.valueOf(shareInfoReq.getType()),
                    fromno, 28, Integer.valueOf(shareInfoReq.getShowtype()));
            int size = userInfoRespList.size();
            if (size > 0) {
                if (querytype == Constants.BROWSE)//根据时间排序
                {
                    userInfoListResp.setFromno(userInfoRespList.get(size - 1).getDate());
                } else if (querytype == Constants.PRAISE)//根据id排序
                {
                    userInfoListResp.setFromno(userInfoRespList.get(size - 1).getMintime());
                }
            }
            userInfoListResp.setCkresult("0");
            userInfoListResp.setDatalist(userInfoRespList);
            userInfoListResp.setErrcode("0");
            userInfoListResp.setErrmsg("success");
        } catch (Exception e) {
            e.printStackTrace();
            userInfoListResp.setCkresult("1");
            throw new ServiceException(DBExceptionEnums.SERVER_INTEERFACE_ERROR);
        }
        String repjson = JsonUtil.toJson(userInfoListResp);
        return response(userInfoListResp);
    }

    /**
     * 邻友、手艺人、关注
     *
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/findfriend", method = {RequestMethod.POST})
    public String getStoreArtUserList( @RequestBody UserInfoReq userInfoReq,
                                      HttpServletRequest request, HttpServletResponse response) {
        UserInfoListResp userInfoListResp = new UserInfoListResp();
        try {
            userInfoListResp.setResult("0");
            String fromno = userInfoReq.getFromno();
            int sftype = Integer.valueOf(userInfoReq.getSftype());
            if (fromno.equals("-1")) {
                if (sftype == 1 || sftype == 2) {
                    fromno = "0";
                } else if (sftype == 3 || sftype == 4 || sftype == 5) {
                    fromno = DateUtils.getCurDateTime();
                    //fromno = String.valueOf(Integer.MAX_VALUE);
                }
                if (userInfoReq.getType().equals("2")) {
                    fromno = "0";
                }
            }
            List<UserInfoResp> userInfoRespList = userActionBusinessService.getNearuserAndShopInfoList(
                    Integer.valueOf(userInfoReq.getUserid()), Integer.valueOf(userInfoReq.getShopid()), Integer.valueOf(userInfoReq.getSftype()),
                    Integer.valueOf(userInfoReq.getType()), fromno, 20, userInfoReq.getLongitude(), userInfoReq.getLatitude());
            int size = userInfoRespList.size();
            if (size > 0) {
                if (sftype == 1 || sftype == 2)//经纬度查询，需要跳跃的行数
                {
                    if (fromno.equals("-1")) {
                        fromno = "20";
                    } else {
                        fromno = String.valueOf((Integer.valueOf(fromno) + 20));
                    }
                } else if (sftype == 3 || sftype == 4 || sftype == 5) {
                    //fromno = String.valueOf(userInfoRespList.get(size-1).getMintime());
                    fromno = DateUtils.transDateToTimeStamp(String.valueOf(userInfoRespList.get(size - 1).getDate())).toString();
                }
                if (userInfoReq.getType().equals("2")) {
                    fromno = userInfoRespList.get(size - 1).getUserid();
                }
            }
            userInfoListResp.setFromno(fromno);
            userInfoListResp.setCkresult("0");
            userInfoListResp.setDatalist(userInfoRespList);
            userInfoListResp.setErrcode("0");
            userInfoListResp.setErrmsg("success");
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(DBExceptionEnums.SERVER_INTEERFACE_ERROR);
        }
        String repjson = JsonUtil.toJson(userInfoListResp);
        return response(userInfoListResp);
    }


}
